import '../css/common.less';
import '../css/article.less';
import '../css/comment.less';
import {HideBtn} from './components/hidebtn.js';
import {appJs} from './components/appjs.js';

let articleId = $("article").attr("data-id");
let UA = navigator.userAgent.toLowerCase();
/* 判断是否存在测评 */
if(isExist('#test')) {
    /* 根据版本号判断添加测评 */
    if (isNew('3.1.0')) {
        //初始化测评按钮滑动显隐
        new HideBtn($('#test'), $(window));
        //判定测评事件
        $('#test, #mastered').on('click', function () {
            new appJs('article.test', articleId);
        });
    }else {
        $('.control, #test').hide();
    }
}


/*点赞为零不显示*/
$('.comment-list').find('.good').find('em').each(function() {
    let $this = $(this);
    if($this.html() == '0') {
        $this.html('');
    }
});

/*文章内图片*/
$('article .content').on('click', 'img', function () {
    new appJs('article.img', $(this).attr('src'));
});

/*相关文章*/
$(".relative").on('click', 'li', function () {
    new appJs('article.articleDetail', $(this).attr('data-id'));
});
/*分享*/
$('.share-list').on('click', 'li', function () {
    new appJs('article.share', articleId, $(this).attr('id')); 
});

/*点赞*/
$('.comment-list').on('click', '.good', function () {
    let $this = $(this);
    let $em = $this.find('em');
    let commentId = $this.parents('.comment-item').attr('data-id');
    if($this.hasClass('active')) {
        $this.removeClass('active animate');
        if(Number($em.html())-1 == 0) {
            $em.html('');
        } else {
             $em.html(Number($em.html())-1);
        }
        new appJs('comment.praiseDelete', commentId);
    } else {
        $em.show();
        $this.addClass('active animate');
        $em.html(Number($em.html())+1);
        new appJs('comment.praiseSave', commentId);
    }
});
/*回复*/
$('.comment-list').on('click', '.reply', function () {
    let $this = $(this);
    let commentId = $this.parents('.comment-item').attr('data-id');
    new appJs('comment.reply', commentId);
});
/*更多评论*/
$('.comment-more').on('click', 'a', function () {
    new appJs('comment.commentList', articleId);
});

/*字体调整方法*/
window.fontSizeChange = function (size) {
    let str = '';
    if (size == '1') {
        str = 'article .content *{font-size: 14px!important;}[data-dpr="2"] article .content *{font-size: 28px!important;}[data-dpr="3"] article .content *{font-size: 42px!important;}article .content{font-size: 14px!important;}[data-dpr="2"] article .content{font-size: 28px!important;}[data-dpr="3"] article .content{font-size: 42px!important;}';
    } else if (size == '2') {
        str = 'article .content *{font-size: 16px!important;}[data-dpr="2"] article .content *{font-size: 32px!important;}[data-dpr="3"] article .content *{font-size: 48px!important;}article .content{font-size: 16px!important;}[data-dpr="2"] article .content{font-size: 32px!important;}[data-dpr="3"] article .content{font-size: 48px!important;}';
    } else if (size == '3') {
        str = 'article .content *{font-size: 18px!important;}[data-dpr="2"] article .content *{font-size: 36px!important;}[data-dpr="3"] article .content *{font-size: 54px!important;}article .content{font-size: 18px!important;}[data-dpr="2"] article .content{font-size: 36px!important;}[data-dpr="3"] article .content{font-size: 54px!important;}';
    } else if(size == '4') {
        str = 'article .content *{font-size: 20px!important;}[data-dpr="2"] article .content *{font-size: 40px!important;}[data-dpr="3"] article .content *{font-size: 60px!important;}article .content{font-size: 20px!important;}[data-dpr="2"] article .content{font-size: 40px!important;}[data-dpr="3"] article .content{font-size: 60px!important;}';
    }
    $('#sizechange').html(str);
}

/*滚动到评论*/
window.toComment = function () {
    let commentTop = parseInt($(".comment").offset().top);
    /*滚动到底部时，滚动条到顶部的距离*/
    let bottom = document.body.scrollHeight-document.documentElement.clientHeight;
    let scrollTo = 0;
    if($(document).scrollTop() == commentTop) {
        scrollTo = 0;
    } else if(bottom < commentTop){
        scrollTo = bottom;
        commentTop = bottom;
    } else {
        scrollTo = commentTop;
    }
    $("body").animate({  
        scrollTop: scrollTo
    }, 250);
}
/* 是否有测评按钮 */
function isExist(ele){
    if($(ele).length >= 1) {
        return true;
    }else {
        return false;
    }
}
/* 版本判断 */
function isNew(newV) {
    if(UA.indexOf('ssydt') == -1) {
        return false;
    }else {
        let _ver = UA.substr(UA.indexOf('ssydt')+5);
        if (_ver >= newV){
            return true;
        }else {
            return false;
        }
    }
}
